package com.zbl.zblproject.erp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.erp.entity.ErpOrder;
import com.zbl.zblproject.erp.entity.ErpTableEntity;
import com.zbl.zblproject.erp.entity.RepertoryVOByYSH;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/10
 */
@Repository
public interface ErpOrderMapper extends BaseMapper<ErpOrder> {

    /**
     * 分页功能
     *
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpOrder> selectPageBySearch(Page page, @Param("search") String search);


    @Select("SELECT SUM(goods_num * goods_price * discount) FROM zbl_erp_orderitem WHERE order_id = #{id}")
    BigDecimal selectOrderTotal(@Param("id") int id);

    /**
     * 获取商品中的第一个商品
     *
     * @param id 订单ID
     * @return 商品ID
     */
    int getGoodsById(int id);

    @Select("SELECT * FROM zbl_erp_order WHERE order_info = #{orderSn}")
    ErpOrder selectErpOrderByOrderSn(@Param("orderSn") String orderSn);

    List<ErpOrder> selectErpOrdersByStatusAndModeAndBrandName(@Param("brandId") int brandId,
                                                              @Param("mode") int mode,
                                                              @Param("status") int status);

    @Select(" SELECT COUNT(*) FROM zbl_erp_order WHERE order_info = #{orderSn} ")
    int selectIsExsitc(@Param("orderSn") String orderSn);


    @Update(" UPDATE zbl_erp_order  SET  status = #{status}  WHERE order_id = #{id} ")
    int updateOrderStatus(@Param("id") int id, @Param("status") int status);

    @Select("SELECT order_info FROM zbl_erp_order WHERE order_id = #{id} ")
    String selectOrderSnById(@Param("id") int id);

    ErpTableEntity selectErpTableEntityByOrderSn(@Param("list") List<String> orderSn);

    @Delete("DELETE FROM zbl_erp_order WHERE order_info = #{orderSn}")
    boolean deleteByOrderSn(@Param("orderSn") String orderSn);

    @Select("SELECT context FROM zbl_erp_order WHERE order_info = #{orderSn}")
    String selectErpOrderContextByOrderSn(@Param("orderSn") String orderSn);

    @Select("SELECT origin_order_sn FROM zbl_erp_order WHERE order_info = #{orderSn}")
    String selectOriginOrderSnByOrderSn(@Param("orderSn") String orderSn);

    List<Integer> getGoodsIds(@Param("start") long start, @Param("end") long end
            , @Param("cat_id") int cat_id, @Param("supplier_id") int supplier_id,
                              @Param("goods_name") String goods_name);

    List<Integer> getGoodsIds1(@Param("start") long start, @Param("end") long end
            , @Param("cat_id") int cat_id, @Param("supplier_id") int supplier_id,
                               @Param("goods_name") String goods_name);

    List<Integer> getGoodsIds2(@Param("start") long start, @Param("end") long end
            , @Param("cat_id") int cat_id, @Param("supplier_id") int supplier_id,
                               @Param("goods_name") String goods_name);

    RepertoryVOByYSH getR(@Param("start") long start, @Param("end") long end, @Param("goods_id") int goods_id);

    @Update("update zbl_erp_order  set logistics_cost_ratio=#{rebate} where order_info=#{orderSn} ")
    int update11(@Param("orderSn") String orderSn, @Param("rebate") BigDecimal rebate);

    @Select("select order_sn from zbl_erp_orderitem  where suppliers_id=#{supId} group by order_sn")
    List<String> getOrderSn(@Param("supId") int supId);

    @Select("select a.* from zbl_erp_order a left join zbl_erp_orderitem b on " +
            "a.order_info=b.order_sn where b.suppliers_id=#{supId} and (a.status=1 or a.status=0) " +
            "group by a.order_info")
    List<ErpOrder> selectBySupId(@Param("supId") int supId);

    List<ErpOrder> exportSearchName(@Param("supId") int supId,@Param("start") String start, @Param("end") String end);
}
